package com.vogue.supplymall.match.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.match.domain.dto.BaseDTO;
import com.vogue.supplymall.match.domain.dto.ProductCustomerDataDTO;
import com.vogue.supplymall.match.domain.dto.ProductDataDTO;
import com.vogue.supplymall.match.domain.dto.ProductPriceDataDTO;
import com.vogue.supplymall.match.domain.service.ColValidateService;
import com.vogue.supplymall.match.domain.service.MatchProductService;

@RestController
public class MatchProductController extends BaseController{
	@Autowired
	private MessageSourceUtils msu;
	@Autowired
	private MatchProductService matchProductService;
	@Autowired
	private ColValidateService colValidateService;
	
	/**
	 *商品信息 
	 */
	@RequestMapping(value="/ns/v1/match/productInfo",method=RequestMethod.POST)
	public String MatchProductBaseInfoController(HttpServletRequest request,HttpServletResponse response,
			@RequestBody(required = true) BaseDTO<List<ProductDataDTO>> baseDTO) {
		
		boolean resultFlag=true;
		String message=msu.getMessage("global.system.success",null);
		Map<String, Object> resultMap=new HashMap<>();
		List<Map<String, Object>> errList=new ArrayList<>();
		
		//首先对数据进行验证
		try {
			errList = colValidateService.productValidate(baseDTO);
			if(errList!=null&&errList.size()>0) {
				resultFlag=false;
				message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
			}else {
				try {
					//保存数据
					matchProductService.matchProductInfo(baseDTO);
				} catch (Exception e) {
					resultFlag=false;
					message=msu.getMessage("match.validate.uperr.product", new String[]{});;
				}
			}
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}
		
		//返回数据
		resultMap.put("result", resultFlag);
		resultMap.put("message", message);
		resultMap.put("details", errList);
		String json=new Gson().toJson(resultMap);
		
		return json;
	}
	
	/**
	 * 商品价格信息
	 */
	@RequestMapping(value="/ns/v1/match/productPriceInfo",method=RequestMethod.POST)
	public String MatchProductPriceController(HttpServletRequest request,HttpServletResponse response,
			@RequestBody(required = true) BaseDTO<List<ProductPriceDataDTO>> baseDTO) {
		
		boolean resultFlag=true;
		String message=msu.getMessage("global.system.success",null);
		Map<String, Object> resultMap=new HashMap<>();
		List<Map<String, Object>> errList=new ArrayList<>();
		
		//首先对数据进行验证
		try {
			errList = colValidateService.producPriceValidate(baseDTO);
			if(errList!=null&&errList.size()>0) {
				resultFlag=false;
				message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
			}else {
				try {
					//保存数据
					matchProductService.matchProductPriceInfo(baseDTO);
				} catch (Exception e) {
					resultFlag=false;
					message=msu.getMessage("match.validate.uperr.productprice", new String[]{});;
				}
			}
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}
		
		//返回数据
		resultMap.put("result", resultFlag);
		resultMap.put("message", message);
		resultMap.put("details", errList);
		String json=new Gson().toJson(resultMap);
		
		return json;
	}
	/**
	 * 商品与客户对应关系信息
	 */
	@RequestMapping(value="/ns/v1/match/proCusRelation",method=RequestMethod.POST)
	public String MatchProCusRelationController(HttpServletRequest request,HttpServletResponse response,
			@RequestBody(required = true) BaseDTO<List<ProductCustomerDataDTO>> baseDTO) {
		
		boolean resultFlag=true;
		String message=msu.getMessage("global.system.success",null);
		Map<String, Object> resultMap=new HashMap<>();
		List<Map<String, Object>> errList=new ArrayList<>();
		
		//首先对数据进行验证
		try {
			errList = colValidateService.productCustomerValidate(baseDTO);
			if(errList!=null&&errList.size()>0) {
				resultFlag=false;
				message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
			}else {
				try {
					//保存数据
					matchProductService.matchProductCustomerInfo(baseDTO);
				} catch (Exception e) {
					resultFlag=false;
					message=msu.getMessage("match.validate.uperr.productprice", new String[]{});;
				}
			}
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}
		
		//返回数据
		resultMap.put("result", resultFlag);
		resultMap.put("message", message);
		resultMap.put("details", errList);
		String json=new Gson().toJson(resultMap);
		
		return json;
	}
		
}
